// 通过express构建服务器
const express = require("express");
const path = require("path");
const mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/news", (err) => {
  if (err) {
    return err;
  }
  console.log("数据库连接成功");
});
// 建立骨架
let schema = mongoose.Schema({
  title: String,
  detail: String,
  addtime: String,
});

// 和集合建立关系
let newsModel = mongoose.model("lists", schema);

let app = express();
app.set("views", "./views");
app.set("view engine", "ejs");

app.use(express.static(path.join(__dirname, "./static")));

app.get("/", async (req, res) => {
  let data = await newsModel.find();
  console.log(data);
  res.render("index.ejs", { data });
});

app.get("/detail", async (req, res) => {
  let { _id } = req.query;
  let [detailData] = await newsModel.find({ _id });
  console.log(detailData.title);
  res.render("detail.ejs", { detailData });
});
app.listen(8989);
